package com.ptl.bp.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ptl.bp.controller.PortableBatteryController;
import com.ptl.bp.dto.PortableQo;
import com.ptl.bp.entity.BatteryPassport;
import com.ptl.bp.entity.PortableBatteryEntity;
import com.ptl.bp.mapper.PortableBatteryMapper;
import com.ptl.bp.util.BoolUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class PortableBatteryService extends ServiceImpl<PortableBatteryMapper, PortableBatteryEntity> {

    public void add(PortableBatteryEntity portableBatteryEntity) {
        // 检查唯一标识符是否已存在
        // if (existsByBatteryPassportId(portableBatteryEntity.getBatteryPassportId())) {
        //     throw new RuntimeException("电池护照标识符已存在: " + portableBatteryEntity.getBatteryPassportId());
        // }
        // if (existsByBatteryId(portableBatteryEntity.getBatteryId())) {
        //     throw new RuntimeException("电池标识符已存在: " + portableBatteryEntity.getBatteryId());
        // }
        portableBatteryEntity.setDocumentStatus(BatteryPassport.PassportDocumentStatus.DRAFT);
        save(portableBatteryEntity);
        log.info("创建便携式电池成功: {}", portableBatteryEntity.getBatteryPassportId());
    }

    public IPage<PortableBatteryEntity> getPage(Page<PortableBatteryEntity> page, PortableQo portableQo, PortableBatteryController portableBatteryController) {
        LambdaQueryWrapper<PortableBatteryEntity> wrapper = new LambdaQueryWrapper<>();
        if (BoolUtils.toBool(portableQo.getBatteryCategory())) {
            wrapper.eq(PortableBatteryEntity::getBatteryCategory, portableQo.getBatteryCategory());
        }
        if (BoolUtils.toBool(portableQo.getManufacturingLocation())) {
            wrapper.like(PortableBatteryEntity::getManufacturingLocation, portableQo.getManufacturingLocation());
        }
        if (BoolUtils.toBool(portableQo.getManufacturerId())) {
            wrapper.eq(PortableBatteryEntity::getManufacturerId, portableQo.getManufacturerId());
        }

        return page(page, wrapper);
    }
}